package com.demo.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.demo.myapplication.R;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ChapterFragment extends Fragment {
  public final static String EXTRA_POS = "position_args";
  public final static String EXTRA_PATH = "path_args";

  private int chapterOrder;
  private String path;

  private TextView chapterContent;
  private String Tag = ChapterFragment.class.getSimpleName();

  public static ChapterFragment newInstance(Bundle args) {
    ChapterFragment fragment = new ChapterFragment();
    fragment.setArguments(args);
    return fragment;
  }

  private void initIntentData() {
    chapterOrder = getArguments().getInt(EXTRA_POS);
    path = getArguments().getString(EXTRA_PATH);
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_chapter, container, false);
    chapterContent = (TextView) v.findViewById(R.id.chapter_content);
    initIntentData();

    new Thread(new Runnable() {
      @Override
      public void run() {
        readTxtContent(path, chapterOrder, new ReadCallBackListener() {
          @Override
          public void onFinish() {
            super.onFinish();
            final String time = getTime();
            getActivity().runOnUiThread(new Runnable() {
              @Override
              public void run() {
                Snackbar.make(chapterContent, "coast " + time + "s", Snackbar.LENGTH_LONG).show();
                chapterContent.setText(content);
              }
            });
          }
        });
      }
    }).start();
    return v;
  }

  private StringBuffer content;

  private void readTxtContent(final String path, int position, final ReadCallBackListener listener) {
    listener.onBegin();
    final int chapterOrder = position + 1;
    //读取文件的第chapterOrder个章节名和第chapterOrder+1个章节名之间内容
    FileInputStream fis;
    BufferedReader br = null;
    try {
      fis = new FileInputStream(path);
      br = new BufferedReader(new InputStreamReader(fis, "utf8"));
      String lineStr;
      int index = 0;
      content = new StringBuffer();
      while ((lineStr = br.readLine()) != null) {
        if (lineStr.startsWith(FileFragment.chapterTitlePrefix) || index == 0) {
          index++;
          if (index == chapterOrder) {
            Log.d(Tag, "get the right index=" + index + ",continue");
            continue;
          }
        }
        if (index == chapterOrder) {
          content.append(lineStr).append("\n");
        }
        if (index == chapterOrder + 1) {
          Log.d(Tag, "get content over");
          break;
        }
      }
      listener.onFinish();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (br != null) {
          br.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}